/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author T.Ron
 */
public class BanquetJpaController implements Serializable {

    public BanquetJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Banquet banquet) throws PreexistingEntityException, Exception {
        if (banquet.getBanquetPK() == null) {
            banquet.setBanquetPK(new BanquetPK());
        }
        banquet.getBanquetPK().setIdCtBq(banquet.getComptet().getIdCt());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comptet comptet = banquet.getComptet();
            if (comptet != null) {
                comptet = em.getReference(comptet.getClass(), comptet.getIdCt());
                banquet.setComptet(comptet);
            }
            em.persist(banquet);
            if (comptet != null) {
                comptet.getBanquetCollection().add(banquet);
                comptet = em.merge(comptet);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findBanquet(banquet.getBanquetPK()) != null) {
                throw new PreexistingEntityException("Banquet " + banquet + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Banquet banquet) throws NonexistentEntityException, Exception {
        banquet.getBanquetPK().setIdCtBq(banquet.getComptet().getIdCt());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Banquet persistentBanquet = em.find(Banquet.class, banquet.getBanquetPK());
            Comptet comptetOld = persistentBanquet.getComptet();
            Comptet comptetNew = banquet.getComptet();
            if (comptetNew != null) {
                comptetNew = em.getReference(comptetNew.getClass(), comptetNew.getIdCt());
                banquet.setComptet(comptetNew);
            }
            banquet = em.merge(banquet);
            if (comptetOld != null && !comptetOld.equals(comptetNew)) {
                comptetOld.getBanquetCollection().remove(banquet);
                comptetOld = em.merge(comptetOld);
            }
            if (comptetNew != null && !comptetNew.equals(comptetOld)) {
                comptetNew.getBanquetCollection().add(banquet);
                comptetNew = em.merge(comptetNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                BanquetPK id = banquet.getBanquetPK();
                if (findBanquet(id) == null) {
                    throw new NonexistentEntityException("The banquet with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(BanquetPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Banquet banquet;
            try {
                banquet = em.getReference(Banquet.class, id);
                banquet.getBanquetPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The banquet with id " + id + " no longer exists.", enfe);
            }
            Comptet comptet = banquet.getComptet();
            if (comptet != null) {
                comptet.getBanquetCollection().remove(banquet);
                comptet = em.merge(comptet);
            }
            em.remove(banquet);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Banquet> findBanquetEntities() {
        return findBanquetEntities(true, -1, -1);
    }

    public List<Banquet> findBanquetEntities(int maxResults, int firstResult) {
        return findBanquetEntities(false, maxResults, firstResult);
    }

    private List<Banquet> findBanquetEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Banquet.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Banquet findBanquet(BanquetPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Banquet.class, id);
        } finally {
            em.close();
        }
    }

    public int getBanquetCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Banquet> rt = cq.from(Banquet.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
